package com.gorkr.multi;

import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author gorkr
 * @date 2022/09/17 11:06
 **/
public class E1114PrintInOrder {

//    private static Semaphore seam_first_two = new Semaphore(0);
//    private static Semaphore seam_two_last = new Semaphore(0);
//
//
//    public E1114PrintInOrder() {
//
//    }
//
//    public void first(Runnable printFirst) throws InterruptedException {
//
//        // printFirst.run() outputs "first". Do not change or remove this line.
//        printFirst.run();
//        seam_first_two.release();
//    }
//
//    public void second(Runnable printSecond) throws InterruptedException {
//
//        seam_first_two.acquire();
//        // printSecond.run() outputs "second". Do not change or remove this line.
//        printSecond.run();
//        seam_two_last.release();
//    }
//
//    public void third(Runnable printThird) throws InterruptedException {
//
//        seam_two_last.acquire();
//
//        // printThird.run() outputs "third". Do not change or remove this line.
//        printThird.run();
//    }

    private boolean firstFinished;
    private boolean secondFinished;
    private static final Object lock = new Object();

    public E1114PrintInOrder() {

    }

    public void first(Runnable printFirst) throws InterruptedException {

        synchronized (lock) {
            // printFirst.run() outputs "first". Do not change or remove this line.
            printFirst.run();
            firstFinished = true;
            lock.notifyAll();
        }
    }

    public void second(Runnable printSecond) throws InterruptedException {

        synchronized (lock) {
            while (!firstFinished) {
                lock.wait();
                lock.notifyAll();
            }

            // printSecond.run() outputs "second". Do not change or remove this line.
            printSecond.run();
            secondFinished = true;
            lock.notifyAll();
        }
    }

    public void third(Runnable printThird) throws InterruptedException {

        synchronized (lock) {
            while (!secondFinished) {
                lock.wait();
                lock.notifyAll();
            }

            // printThird.run() outputs "third". Do not change or remove this line.
            printThird.run();
        }
    }




    @Test
    public void Test() {

        E1114PrintInOrder e1114PrintInOrder = new E1114PrintInOrder();
        ExecutorService executor = Executors.newCachedThreadPool();
        executor.submit(() -> {
            try {
                e1114PrintInOrder.first(() -> {
                    System.out.println("1");
                });
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        executor.submit(() -> {
            try {
                e1114PrintInOrder.second(() -> {
                    System.out.println("2");
                });
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });



        executor.submit(() -> {
            try {
                e1114PrintInOrder.third(() -> {
                    System.out.println("3");
                });
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
    }
}




    